package com.ymxk.livedata;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.timestar.event.LiveDataBus;

public class MainActivity extends AppCompatActivity {

    private TextView tvContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        tvContent = findViewById(R.id.tvContent);

        findViewById(R.id.tvButton).setOnClickListener(view -> {
            //LiveData1Activity/LiveData2Activity
            LiveData1Activity.jumpTo(this);
        });

        LiveDataBus.getDefault().register(this, eventData -> {
            int what = eventData.what;
            Toast.makeText(this,
                    eventData.obj + "-->" + what,
                    Toast.LENGTH_SHORT).show();
            tvContent.setText(eventData.obj.toString());
        });

    }
}